<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/24 0024
 * Time: 上午 11:52
 * Author: NanTao <260053530@qq.com>
 */

namespace app\bjwlmanager\controller;
use think\Db;
class Finance extends ManagerBasic
{
    public function cash()
    {
        $status = input('param.status');
        $keywords = input('param.keywords');
        $logmin = input('param.logmin');
        $logmax = input('param.logmax');
        if (isset($status) && $status != '')
        {
            $where['a.status'] = $status;
            $this->assign('status',$status);
        }
        $keywords ? $where['b.user_name'] = ['like',"%{$keywords}%"] :'';
        $logmin ? $where['a.add_time'] = ['>',$logmin] :'';
        $logmax ? $where['a.add_time'] = ['<',$logmax] :'';
        if ($logmin && $logmax) {
            $where['a.add_time'] = ['between',[$logmin,$logmax]];

        }
        $list = Db::name('user_cash')->alias('a')->field('a.*,b.user_nickname,b.true_name')->join('user b','a.user_id=b.id')->where($where)->order('a.add_time desc')->paginate(15,false,['query'=>request()->param()]);
        $this->assign('list',$list);

        $money = Db::name('user_cash')->alias('a')->field('a.*,b.user_name')->join('user b','a.user_id=b.id')->where($where)->sum('a.money');
        $cost = Db::name('user_cash')->alias('a')->field('a.*,b.user_name')->join('user b','a.user_id=b.id')->where($where)->sum('a.cash_cost');
        $tax = Db::name('user_cash')->alias('a')->field('a.*,b.user_name')->join('user b','a.user_id=b.id')->where($where)->sum('a.cash_tax');
        $cashMoney = Db::name('user_cash')->alias('a')->field('a.*,b.user_name')->join('user b','a.user_id=b.id')->where($where)->sum('a.cash_money');
        $this->assign('money',$money);
        $this->assign('cost',$cost);
        $this->assign('tax',$tax);
        $this->assign('cashMoney',$cashMoney);
        return $this->fetch();
    }
    public function del()
    {
        $cashId = input('param.cashId');
        if (Db::name('user_cash')->delete($cashId))
        {
            json_msg(0,'删除成功');
        }
    }
    public function make()
    {
        $action = input('param.action');
        $cashId = input('param.cashId');
        $cash = Db::name('user_cash')->find($cashId);
        switch ($action){
            case 'success':
                Db::name('user')->where('id',$cash['user_id'])->setDec('frozen',$cash['money']);
                $result = Db::name('user_cash')->where('id',$cashId)->setField('status',1);
                break;
            case 'fail':
                Db::name('user')->where('id',$cash['user_id'])->setInc('balance',$cash['money']);

                post_user_balance_log($cash['user_id'],'+',$cash['money'],"申请提现失败,退回".$cash['money']."元",4,$cashId);
                $result = Db::name('user_cash')->where('id',$cashId)->setField('status',2);
                break;
        }
        if ($result || $result==0)
        {
            json_msg(0,'操作成功');
        } else {
            json_msg(1,'操作失败');
        }
    }
    /*购买记录*/
    public function buy()
    {
        $where = ['b.status'=>['gt','0']];
        $keywords = input('param.keywords');
        $logmin = input('param.logmin');
        $logmax = input('param.logmax');
        $goodsId = input('param.goodsId');
        $keywords ? $where['a.order_no|a.goods_title'] = ['like',"%{$keywords}%"] :'';
        $logmin ? $where['b.add_time'] = ['>',$logmin] :'';
        $logmax ? $where['b.add_time'] = ['<',$logmax] :'';
        if ($logmin && $logmax) {
            $where['b.add_time'] = ['between',[$logmin,$logmax]];

        }
        $goodsId ? $where['a.goods_id']= trim($goodsId) : '';

        $list = Db::name('goods_order_detail a')
                ->field('a.*,c.user_nickname,c.true_name,b.add_time')
                ->join('goods_order b','a.order_id=b.id')
                ->join('user c','a.user_id=c.id')
                ->where($where)->order('b.add_time desc')
                ->paginate(15,false,['query'=>request()->param()]);
        $this->assign('list',$list);

        $countSum = Db::name('goods_order_detail a')
                ->field('a.*,c.user_nickname,c.true_name,b.add_time')
                ->join('goods_order b','a.order_id=b.id')
                ->join('user c','a.user_id=c.id')
                ->where($where)->order('b.add_time desc')
                ->sum('a.goods_count');


        $list1 = Db::name('goods_order_detail a')
                ->field('a.*,c.user_nickname,c.true_name,b.add_time')
                ->join('goods_order b','a.order_id=b.id')
                ->join('user c','a.user_id=c.id')
                ->where($where)->order('b.add_time desc')
                ->select();
                $priceSum = '';
            foreach ($list1 as $key => $value) {
                $priceSum += ($value['goods_sell_price'] * $value['goods_count']);
            }



        $this->assign('countSum',$countSum);
        $this->assign('priceSum',$priceSum);

        return $this->fetch();
    }
    /*返佣记录*/
    public function distribution()
    {
        $keywords = input('param.keywords');
        $logmin = input('param.logmin');
        $logmax = input('param.logmax');
        $keywords ? $where['c.user_name|c.true_name|c.user_nickname|b.user_name|b.true_name|b.user_nickname|a.order_no'] = ['like',"%{$keywords}%"] :'';
        $logmin ? $where['a.add_time'] = ['>',$logmin] :'';
        $logmax ? $where['a.add_time'] = ['<',$logmax] :'';
        if ($logmin && $logmax) {
            $where['a.add_time'] = ['between',[$logmin,$logmax]];

        }
        $list = Db::name('user_distribution_log')
            ->alias('a')->field('a.*,b.user_nickname,b.true_name,c.user_nickname add_user_nickname,c.true_name add_true_name')
            ->join('user b','a.user_id=b.id')
            ->join('user c','a.add_user_id=c.id')
            ->where($where)
            ->order('a.add_time desc')
            ->paginate(15,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $distSum = Db::name('user_distribution_log')
            ->alias('a')->field('a.*,b.user_nickname,b.true_name,c.user_nickname add_user_nickname,c.true_name add_true_name')
            ->join('user b','a.user_id=b.id')
            ->join('user c','a.add_user_id=c.id')
            ->where($where)
            ->sum('a.add_money');
        $this->assign('distSum',$distSum);
        return $this->fetch();
    }

    /*导出execl*/
    public function export()
    {
        $header = [
            '编号',
            '订单编号',
            '购买者ID',
            '购买者昵名',
            '购买金额',
            '上级ID',
            '上级昵称',
            '商品ID',
            '商品标题',
            '商品价格',
            '代理层级',
            '添加时间',
        ];
        $list = Db::name('user_distribution_log')
            ->alias('a')->field('a.*,b.user_nickname,b.true_name,c.user_nickname add_user_nickname,c.true_name add_true_name')
            ->join('user b','a.user_id=b.id')
            ->join('user c','a.add_user_id=c.id')
            ->select();
        $data = [];
        foreach ($list as $k=>$v)
        {
            $userName = $v['true_name']?$v['true_name']:$v['user_nickname'];
            $addUserName = $v['add_true_name']?$v['add_true_name']:$v['add_user_nickname'];
            $data[$k]=[$v['id'],$v['order_no'],$v['user_id'],$userName,$v['add_money'],$v['add_user_id'],$addUserName,$v['goods_id'],$v['goods_title'],$v['goods_sell_price'],$v['level'],$v['add_time']];
        }

        $model = controller('Excel');

        $model->writer($header, $data);

        json_msg(0,'导出完成');
    }
}